import axios from 'axios'
import React, { useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { Button, Input, Form } from 'react-vant'
import { Notify } from 'react-vant'
const Index = () => {
  const [form] = Form.useForm()
  const navigate = useNavigate()
  const onFinish = values => {
    console.log(values)
    axios.post('/xxx/chat/login', values).then((resp) => {
      if(resp.data.status === '0000') {
        navigate('/chat/robot', { replace: true })
        sessionStorage.setItem('token', resp.headers.token)
        Notify.show({ type: 'success', message: resp.data.message })
      }  else {
        Notify.show({ type: 'danger', message: resp.data.message })
      }
    })
  }
  const token = sessionStorage.getItem('token');
  
  useEffect(() => {
    if(token) {
      navigate('/chat/robot', { replace: true })
    }
  }, [token])
  return (
    <Form
      form={form}
      onFinish={onFinish}
      footer={
        <div style={{ margin: '16px 16px 0' }}>
          <Button round nativeType='submit' type='primary' block>
            提交
          </Button>
        </div>
      }
    >
      <Form.Item
        tooltip={{
          message:
            'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.',
        }}
        intro='没有账号自动注册'
        rules={[{ required: true, message: '请填写用户名' }]}
        name='loginId'
        label='用户名'
      >
        <Input placeholder='请输入用户名' />
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: '请填写密码' }]}
        name='loginPwd'
        label='密码'
      >
        <Input placeholder='请输入密码' />
      </Form.Item>
    </Form>
  )
}


export default Index